#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

bool my_isalpha(string &word){
    for(auto it=word.begin();it!=word.end();it++){
        if(!isalpha(*it))
            return false;
    }
    return true;
}

class Item{
public:
    
    Item(string &k): word(k){}

    string word;
    int freq=1;
};

class Content{
public:
    Content(){
        vec.reserve(1000);
    }

    void readFile(string &filename){
        ifstream ifs(filename);

        if(!ifs.good()){
            cerr<<" ReadFile ERROR "<<endl;
            return ;
        }

        string word;
        while(ifs>>word){
            if(my_isalpha(word)){
                put(word);
            }
        }
    }

    void storeFile(string &filename){
        ofstream ofs(filename);
        
        for(auto it=vec.begin();it!=vec.end();it++){
            ofs<<(*it).word<<" "<<(*it).freq<<endl;
        }

        cout<<" -- Store Done -- "<<endl;

    }

    void put(string &word){
        for(size_t i=0;i<vec.size();i++){
            if(vec[i].word==word){
                vec[i].freq++;
                return;
            }
        }
        vec.emplace_back(word);
    }

    int get(string &word){
        for(auto it=vec.begin();it!=vec.end();it++ ){
            if((*it).word==word){
                return (*it).freq;
            }
        }
        return 0;
    }

private:
    vector<Item> vec;

};

void test(){
    Content con;
    string readfile="./The_Holy_Bible.txt";
    con.readFile(readfile);

    string storefile="./Bible_WordCount_1.txt";
    con.storeFile(storefile);

    string getWord="find";
    cout<<" -- "<<getWord<<" : "<<con.get(getWord)<<" times -- "<<endl;

}

int main()
{
 test();
 return 0;
}

